//
// Created by Dinger on 2021/10/2.
//

#include "../Header files/semantic_form.h"

Item::Item() {
    _id = 0;
    _isAssignment = false;
}

Item::Item(int id, string type, string name) {
    _id = id;
    _type = type;
    _name = name;
    _isAssignment = false;
}

void Item::outputItem() {
    cout << "ID:" << _id << ",type:" << _type << ",name:" << _name << ",value:" << _value << endl;
}

ItemForm::ItemForm() {
    _size = 0;
}

void ItemForm::insertItemForm(string type, string name) {
    Item item(_size++, type, name);
    _itemForm.insert(pair<string, Item>(name, item));
}

void ItemForm::changeItemValue(string name, int value) {
    _itemForm[name].setValue(value);
    _itemForm[name].setIsAssignment(true);
}

bool ItemForm::isItemDefine(string name) {
    map<string, Item>::iterator it = _itemForm.find(name);
    if (it == _itemForm.end()) {
        return false;
    }
    return true;
}

QuaternaryFormula::QuaternaryFormula(string firstFormula, string secondFormula, string thirdFormula,
                                     string forthFormula) {
    _firstFormula = firstFormula;
    _secondFormula = secondFormula;
    _thirdFormula = thirdFormula;
    _forthFormula = forthFormula;
    makeOutputString();
}

QuaternaryFormula::QuaternaryFormula(string number, string name) {
    _firstFormula = ":=";
    _secondFormula = number;
    _thirdFormula = "#";
    _forthFormula = name;
    makeOutputString();
}

void QuaternaryFormula::makeOutputString() {
    _outputString.clear();
    if (!_label.empty()) {
        _outputString += _label + ":\n";
    }
    _outputString += "(" + _firstFormula + "\t" + _secondFormula + "\t" + _thirdFormula + "\t" + _forthFormula + ")";
}
